package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTConfig;
import com.lb.jwitter.service.TblCTDTConfigService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTiet;
import com.lb.jwitter.service.TblDKMHDotDKHocVienChiTietService;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHKetqua;
import com.lb.jwitter.service.TblDKMHKetquaService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class GetLichTrinhDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private int dotDKId;

	public int getDotDKId() {
		return dotDKId;
	}

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}
	
	private boolean trongHanDK;

	public boolean isTrongHanDK() {
		return trongHanDK;
	}

	public void setTrongHanDK(boolean trongHanDK) {
		this.trongHanDK = trongHanDK;
	}
	private int dotDKHocVienId;

	public int getDotDKHocVienId() {
		return dotDKHocVienId;
	}

	public void setDotDKHocVienId(int dotDKHocVienId) {
		this.dotDKHocVienId = dotDKHocVienId;
	}

	private List<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets;

	public List<TblDKMHDotDKHocVienChiTiet> getTblDKMHDotDKHocVienChiTiets() {
		return tblDKMHDotDKHocVienChiTiets;
	}

	public void setTblDKMHDotDKHocVienChiTiets(
			List<TblDKMHDotDKHocVienChiTiet> tblDKMHDotDKHocVienChiTiets) {
		this.tblDKMHDotDKHocVienChiTiets = tblDKMHDotDKHocVienChiTiets;
	}

	private String quyenDK;
	public String getQuyenDK() {
		return quyenDK;
	}

	public void setQuyenDK(String quyenDK) {
		this.quyenDK = quyenDK;
	}
	
	private Date today;

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    TblDKMHKetquaService tblDKMHKetquaService = (TblDKMHKetquaService) context.getBean("tbl_DKMH_KetquaClient");
	    TblDKMHDotDKHocVienChiTietService tblDKMHDotDKHocVienChiTietService = (TblDKMHDotDKHocVienChiTietService)  context.getBean("tbl_DKMH_DotDK_HocVien_ChiTietClient");
	    TblThongTinTKBChiTietService tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService)  context.getBean("tbl_ThongTin_TKB_ChiTietClient");
		TblCTDTConfigService tblCTDTConfigService = (TblCTDTConfigService) context.getBean("tbl_CTDT_ConfigClient");
	    
		if(tblDKMHTaiKhoanHocVien != null){
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKHocVienId.id");
		        con.setValue1(new Long(dotDKHocVienId));
		        con.setOperator("eq");
		        cons.add(con);	
		    }
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("ngayBatDau");
		        con.setValue1("asc");
		        con.setOperator("order");
		        cons.add(con);	
		    }
		    tblDKMHDotDKHocVienChiTiets = tblDKMHDotDKHocVienChiTietService.findtblDKMHDotDKHocVienChiTietByCondition(cons, 0, 0);
		    
		    
		    
		    int soTCDangKyToiDa = 21;
		    int soTCDieuChinh = 4;
		    today = new Date();
		    trongHanDK = false;
		    QuyenDangKy quyenDK = new QuyenDangKy();
		    for(TblDKMHDotDKHocVienChiTiet tbl : tblDKMHDotDKHocVienChiTiets){
		    	
		    	if(today.after(tbl.getNgayBatDau().toGregorianCalendar().getTime()) 
		    			&& today.before(tbl.getNgayKetThuc().toGregorianCalendar().getTime())){
		    		
		    		trongHanDK = true;
		    		//soTCDangKyToiDa = tbl.getDotDKHocVienId().getSoTCDangKyToiDa();
		    		if(tbl.getQuyen().toUpperCase().charAt(0) == 'W')
		    			quyenDK.setInsert(true);

		    		if(tbl.getQuyen().toUpperCase().charAt(1) == 'U')
		    			quyenDK.setUpdate(true);

		    		if(tbl.getQuyen().toUpperCase().charAt(2) == 'D')
		    			quyenDK.setDelete(true);
			    	
		    		
		    		/*
		    		 * Lay ra so TC DK toi da cua sinh vien
		    		 * Neu so TC Toida trong dot DK >0 --> lay soTCTD trong dotDK
		    		 * Neu soTCTD trong dotDk ==0 - > kiem tra so TCTD cua Khoahoc va HeDaotao trong bang ctdt_config
		    		 * Neu khong co trong bang CTDT_Config thi lay mac dinh la 21 tc
		    		 */
		    		try {
	    				cons.clear();
		    			{ 
		    		        ConditionReport con = new ConditionReport(); 
		    		        con.setCol("heDaoTaoId.id");
		    		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getHeDaoTaoId().getId());
		    		        con.setOperator("eq");
		    		        cons.add(con);	
		    		    }
		    			{ 
		    		        ConditionReport con = new ConditionReport(); 
		    		        con.setCol("khoaHoc");
		    		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc());
		    		        con.setOperator("eq");
		    		        cons.add(con);	
		    		    }
		    			List<TblCTDTConfig> tblCTDTConfigs = tblCTDTConfigService
		    													.findtblCTDTConfigByCondition(cons, 1, 1);
		    			if(tblCTDTConfigs != null && tblCTDTConfigs.size() > 0){
		    				soTCDangKyToiDa = tblCTDTConfigs.get(0).getSoTCHKMax();
		    				soTCDieuChinh = tblCTDTConfigs.get(0).getSoTCDieuChinh();
		    			}
					} catch (Exception e) {}
		    		
		    		//kiem tra soTCTD trong dotDK cua HV
		    		if(tbl.getDotDKHocVienId().getSoTCDangKyToiDa() != null && tbl.getDotDKHocVienId().getSoTCDangKyToiDa() > 0){
		    			soTCDangKyToiDa = tbl.getDotDKHocVienId().getSoTCDangKyToiDa();
		    		}
			    	
		    	}
		    	
		    	
		    }
		    

		    
		    //tao session ghi nhan thong tin dang ky va kq dang ky
		    cons.clear();
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKId.id");
		        con.setValue1(new Long(dotDKId));
		        con.setOperator("eq");
		        cons.add(con);	
		    }
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("hocVienId.id");
		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getId());
		        con.setOperator("eq");
		        cons.add(con);	
		    }
		    
		    List<TblDKMHKetqua> tblDKMHKetquas = tblDKMHKetquaService.findtblDKMHKetquaByCondition(cons, 0, 0);
		    
		    List<KetQuaDangKy> ketQuaDangKys = new ArrayList<KetQuaDangKy>();
		    for(TblDKMHKetqua ketQua : tblDKMHKetquas){
		    	if(!ketQua.isXacNhanYN() || 
		    			(ketQua.isXacNhanYN() && !ketQua.getMaDieuChinh().equals("BH") && !ketQua.getMaDieuChinh().equals("HH"))){
		    	
			    	KetQuaDangKy ketQuaDangKy = new KetQuaDangKy();
			    	
			    	ThoiKhoaBieuDuKien thoiKhoaBieuDuKien = new ThoiKhoaBieuDuKien();
			    	thoiKhoaBieuDuKien.setNhomLop(ketQua.getNLMHId());
			    	
			    	cons.clear();
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("TKBId.id");
				        con.setValue1(ketQua.getNLMHId().getTKBId().getId());
				        con.setOperator("eq");
				        cons.add(con);	
				    }
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("ngayHocId.STT");
				        con.setValue1("asc");
				        con.setOperator("order");
				        cons.add(con);	
				    }
			    	thoiKhoaBieuDuKien.setChiTiets(tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0));
			    	
			    	ketQuaDangKy.setKetqua(ketQua);
			    	ketQuaDangKy.setThoiKhoaBieuDuKien(thoiKhoaBieuDuKien);
			    	ketQuaDangKys.add(ketQuaDangKy);
		    	}
		    }

		    
		    //tao cac session
		    session.put("dotDKId", dotDKId);
		    session.put("soTCDangKyToiDa", soTCDangKyToiDa);
		    session.put("soTCDieuChinh", soTCDieuChinh);
		    session.put("trongHanDK", trongHanDK);

		    session.put("quyenDK", quyenDK);
		    session.put("ketQuaDangKys", ketQuaDangKys);
		}
		return Action.SUCCESS;

	}


	
	
	
	
	
}
